All files / src/components/user ContentRail.tsx

0% Statements 0/32
0% Branches 0/29
0% Functions 0/7
0% Lines 0/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184                                                                                                                                                                                                                                                                                                                                                                               
'use client';
 
import React, { useRef, useState } from 'react';
import { ChevronLeft, ChevronRight, ArrowRight } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import Link from 'next/link';
 
import { cn } from '@/lib/utils';
import type { DiscoveryMediaItem } from '@/types';
import { ContentCard } from './ContentCard';
import { Button } from '@/components/ui/button';
 
interface ContentRailProps {
  title: string;
  subtitle?: string;
  icon?: React.ReactNode;
  items: DiscoveryMediaItem[];
  isLoading?: boolean;
  emptyLabel?: string;
  viewAllHref?: string;
  aspectRatio?: 'poster' | 'video';
  onEndReached?: () => void;
}
 
export function ContentRail({
  title,
  subtitle,
  icon,
  items,
  isLoading = false,
  emptyLabel,
  viewAllHref,
  aspectRatio = 'poster'}: ContentRailProps) {
  const { t } = useTranslation();
  const scrollContainerRef = useRef<HTMLDivElement>(null);
  const [showLeftArrow, setShowLeftArrow] = useState(false);
  const [showRightArrow, setShowRightArrow] = useState(true);
 
  const handleScroll = () => {
    if (scrollContainerRef.current) {
      const { scrollLeft, scrollWidth, clientWidth } = scrollContainerRef.current;
      setShowLeftArrow(scrollLeft > 0);
      setShowRightArrow(scrollLeft < scrollWidth - clientWidth - 10);
    }
  };
 
  const scroll = (direction: 'left' | 'right') => {
    if (scrollContainerRef.current) {
      const container = scrollContainerRef.current;
      const scrollAmount = container.clientWidth * 0.8;
      
      container.scrollBy({
        left: direction === 'left' ? -scrollAmount : scrollAmount,
        behavior: 'smooth'
      });
    }
  };
 
  // Skeleton Loading State
  if (isLoading) {
    const skeletonCount = 6;
    const widthClass = aspectRatio === 'poster' ? 'w-[160px] sm:w-[180px] md:w-[200px]' : 'w-[260px] sm:w-[280px] md:w-[320px]';
    const aspectClass = aspectRatio === 'poster' ? 'aspect-[2/3]' : 'aspect-[16/9]';
    
    return (
      <section className="space-y-4 py-4 relative group/rail">
        <div className="flex items-center gap-3 px-6 sm:px-10">
           {/* Skeleton Title */}
           <div className="h-7 w-48 bg-slate-800/50 rounded-md animate-pulse" />
        </div>
        
        <div className="flex gap-4 overflow-hidden px-6 sm:px-10 pb-4">
          {Array.from({ length: skeletonCount }).map((_, i) => (
            <div 
              key={`skel-${i}`} 
              className={cn(
                "shrink-0 bg-slate-800/40 rounded-lg animate-pulse",
                widthClass,
                aspectClass
              )} 
            />
          ))}
        </div>
      </section>
    );
  }
 
  if (!items || items.length === 0) {
    // Optional: Render nothing or an empty state
    if (!emptyLabel) return null;
    
    return (
      <section className="space-y-4 py-6 px-6 sm:px-10">
        <div className="flex items-center gap-2 mb-4">
          {icon}
          <h3 className="text-xl font-bold text-white">{title}</h3>
        </div>
        <div className="p-8 rounded-xl border border-dashed border-slate-800 bg-slate-900/30 text-center">
          <p className="text-slate-400">{emptyLabel}</p>
        </div>
      </section>
    );
  }
 
  return (
    <section className="space-y-4 py-2 group/rail relative">
      {/* Header */}
      <div className="flex items-end justify-between px-6 sm:px-10 mb-2">
        <div className="space-y-1">
          <div className="flex items-center gap-2">
            {/* Accent Line */}
            <div className="w-1 h-6 bg-blue-600 rounded-full mr-1" />
            <h3 className="text-xl sm:text-2xl font-bold text-white tracking-tight drop-shadow-sm">
              {title}
            </h3>
            {icon && <span className="text-slate-400 ml-1 opacity-80">{icon}</span>}
          </div>
          {subtitle && (
            <p className="text-sm text-slate-400 font-medium pl-4">{subtitle}</p>
          )}
        </div>
 
        {viewAllHref && (
          <Link 
            href={viewAllHref} 
            className="group/link flex items-center gap-1 text-sm font-semibold text-blue-400 hover:text-blue-300 transition-colors"
          >
            <span>{t('user.common.viewAll')}</span>
            <ArrowRight className="w-4 h-4 transition-transform group-hover/link:translate-x-1" />
          </Link>
        )}
      </div>
 
      {/* Rail Container */}
      <div className="relative">
        {/* Left Scroll Button - Only visible on hover and if scrolled */}
        <button
          onClick={() => scroll('left')}
          className={cn(
            "absolute left-0 top-0 bottom-4 z-20 w-12 sm:w-16 bg-gradient-to-r from-black/80 to-transparent flex items-center justify-center opacity-0 group-hover/rail:opacity-100 transition-opacity duration-300 disabled:opacity-0",
            !showLeftArrow && "hidden"
          )}
          aria-label={t('common.previous')}
        >
          <ChevronLeft className="w-8 h-8 text-white drop-shadow-lg hover:scale-110 transition-transform" />
        </button>
 
        {/* Scrollable Area */}
        <div
          ref={scrollContainerRef}
          onScroll={handleScroll}
          className="flex gap-4 overflow-x-auto pb-8 pt-2 px-6 sm:px-10 scrollbar-none snap-x snap-mandatory"
          style={{ scrollbarWidth: 'none', msOverflowStyle: 'none' }}
        >
          {items.map((item) => (
            <div key={item.id} className="snap-start shrink-0">
              <ContentCard 
                item={item} 
                aspectRatio={aspectRatio} 
                showProgress={item.badges?.includes('continue') || item.progress !== undefined}
              />
            </div>
          ))}
          
          {/* Spacer for end padding */}
          <div className="w-2 shrink-0" />
        </div>
 
        {/* Right Scroll Button */}
        <button
          onClick={() => scroll('right')}
          className={cn(
            "absolute right-0 top-0 bottom-4 z-20 w-12 sm:w-16 bg-gradient-to-l from-black/80 to-transparent flex items-center justify-center opacity-0 group-hover/rail:opacity-100 transition-opacity duration-300 disabled:opacity-0",
            !showRightArrow && "hidden"
          )}
          aria-label={t('common.next')}
        >
          <ChevronRight className="w-8 h-8 text-white drop-shadow-lg hover:scale-110 transition-transform" />
        </button>
      </div>
    </section>
  );
}